﻿-- Author      : Kurapica
-- Create Date : 7/12/2008 14:38
     
-- Button
do
	-- Check Version
	local version = 1
	if not IGAS:RegisterFile("IGAS.GUI.Widget.Button", version) then
		return
	end
    
    local getmetatable = getmetatable
    local pairs = pairs
    local ipairs = ipairs
    local type = type
    local error = error
    local CreateFrame = CreateFrame
    
	local _WidgetName = "Button"
    local _Base = "Frame"

	local _GUI = IGAS.GUI
	local _Widget = IGAS.GUI.Widget
    local _BaseFunc = IGAS.GUI.Base.Func
	local _Enum = IGAS.GUI.Base.Enum
	local _PropertyType = IGAS.GUI.Base.PropertyType
    
    -- Button Template
    local TEMPLATE_NONE = "NONE"
    local TEMPLATE_NORMAL = "NORMAL"
    local TEMPLATE_GRAY = "GRAY"
    local TEMPLATE_CLOSEBTN = "CLOSE"
    
    -- Define Block
    _Enum["ButtonStyle"] = {
        TEMPLATE_NONE,
        TEMPLATE_NORMAL,
        TEMPLATE_GRAY,
        TEMPLATE_CLOSEBTN,
    }
    
    _PropertyType["ButtonStyle"] = {
        ["Type"] = "enum",
		["EnumType"] = _Enum.ButtonStyle,
    }
	
    -- Scripts
    local function onClick_Close(self)
		if not self.Parent.InDesignMode then
			self.Parent.Visible = false
		end
    end
    
	-- ScriptType
	local _ScriptType = {
	}
    
	-- FuncProxy
	local _FuncProxy = {
        ["SetButtonState"] = function(self, state, locked)
            if locked ~= nil then
                self.__Locked = locked
            end
            
            self.__UI:SetButtonState(state, self.__Locked)
        end,
        
        -- Used for ButtonTemplate
        ["SetStyle"] = function(self, style)
            local chk = false
            local t
            
            -- Check Style
            if not style or type(style) ~= "string" then
                return
            end
            
            for _, v in ipairs(_Enum["ButtonStyle"]) do
                if v == style then
                    chk = true
                    break
                end
            end
            
            if (not chk) or style == self.__Style then
                return
            end
			
            self.__Style = style
			
            -- Change Style
            if style == TEMPLATE_NORMAL then
                --- Font
                self:SetNormalFontObject(GameFontNormal)
                self:SetDisabledFontObject(GameFontDisable)
                self:SetHighlightFontObject(GameFontHighlight)
                
                --- Texture
                self:SetNormalTexture("Interface\\Buttons\\UI-Panel-Button-Up")
                t = self:GetNormalTexture()
                t:SetTexCoord(0,0.625,0,0.6875)
                
                self:SetPushedTexture("Interface\\Buttons\\UI-Panel-Button-Down")
                t = self:GetPushedTexture()
                t:SetTexCoord(0,0.625,0,0.6875)
                
                self:SetDisabledTexture("Interface\\Buttons\\UI-Panel-Button-Disabled")
                t = self:GetDisabledTexture()
                t:SetTexCoord(0,0.625,0,0.6875)
                
                self:SetHighlightTexture("Interface\\Buttons\\UI-Panel-Button-Highlight")
                t = self:GetHighlightTexture()
                t:SetTexCoord(0,0.625,0,0.6875)
            elseif style == TEMPLATE_GRAY then
                --- Font
                self:SetNormalFontObject(GameFontHighlight)
                self:SetDisabledFontObject(GameFontDisable)
                self:SetHighlightFontObject(GameFontHighlight)
                
                --- Texture
                self:SetNormalTexture("Interface\\Buttons\\UI-Panel-Button-Disabled")
                t = self:GetNormalTexture()
                t:SetTexCoord(0,0.625,0,0.6875)
                
                self:SetPushedTexture("Interface\\Buttons\\UI-Panel-Button-Disabled-Down")
                t = self:GetPushedTexture()
                t:SetTexCoord(0,0.625,0,0.6875)
                
                self:SetDisabledTexture("Interface\\Buttons\\UI-Panel-Button-Disabled")
                t = self:GetDisabledTexture()
                t:SetTexCoord(0,0.625,0,0.6875)
                
                self:SetHighlightTexture("Interface\\Buttons\\UI-Panel-Button-Highlight")
                t = self:GetHighlightTexture()
                t:SetTexCoord(0,0.625,0,0.6875)
            elseif style == TEMPLATE_CLOSEBTN then
                --- Texture
                self:SetNormalTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Up")
                
                self:SetPushedTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Down")
                                
                self:SetHighlightTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight")
                t = self:GetHighlightTexture()
                t:SetBlendMode("ADD")
                
                self.Width = 32
                self.Height = 32
                self.OnClick = onClick_Close
            end
        end,
        
        ["GetStyle"] = function(self)
            return self.__Style or TEMPLATE_NONE
        end,
    }
    
	-- Property
	local _Property = {
        -- Style
        ["Style"] = {
			["Set"] = function(self, style)
                self:SetStyle(style)
			end,
			
			["Get"] = function(self)
				return self:GetStyle()
			end,
			
			["Type"] = _PropertyType.ButtonStyle,
		},
        
        -- Enabled
        ["Enabled"] = {
			["Set"] = function(self, enabled)
                if enabled then
				    self:Enable()
                else
				    self:Disable()
                end
			end,
			
			["Get"] = function(self)
				return (self:IsEnabled() and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
    
        -- ButtonState
        ["ButtonState"] = {
			["Set"] = function(self, state)
                self:SetButtonState(state, self.__Locked)
			end,
			
			["Get"] = function(self)
				return self:GetButtonState()
			end,
			
			["Type"] = _PropertyType.ButtonState,
		},
        
        -- Locked
        ["Locked"] = {
			["Set"] = function(self, enabled)
                self.__Locked = enabled
                self:SetButtonState(self.__UI:GetButtonState(), enabled)
			end,
			
			["Get"] = function(self)
				return (self.__Locked and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
        
        -- DisabledFontObject
        ["DisabledFontObject"] = {
			["Set"] = function(self, font)
                self:SetDisabledFontObject(font)
			end,
			
			["Get"] = function(self)
				return self:GetDisabledFontObject()
			end,
			
			["Type"] = _PropertyType.FontObject,
		},
     
     	-- DisabledTexture
     	["DisabledTexture"] = {
			["Set"] = function(self, texture)
				self:SetDisabledTexture(texture)
			end,
			
			["Get"] = function(self)
				return self:GetDisabledTexture()
			end,
			
			["Type"] = _PropertyType.Texture,     	
     	},
     	
     	-- HighlightTexture
     	["HighlightTexture"] = {
			["Set"] = function(self, texture)
				self:SetHighlightTexture(texture)
			end,
			
			["Get"] = function(self)
				return self:GetHighlightTexture()
			end,
			
			["Type"] = _PropertyType.Texture,     	
     	},    
    
     	-- NormalTexture
     	["NormalTexture"] = {
			["Set"] = function(self, texture)
				self:SetNormalTexture(texture)
			end,
			
			["Get"] = function(self)
				return self:GetNormalTexture()
			end,
			
			["Type"] = _PropertyType.Texture,     	
     	},    
  	
     	-- PushedTexture
     	["PushedTexture"] = {
			["Set"] = function(self, texture)
				self:SetPushedTexture(texture)
			end,
			
			["Get"] = function(self)
				return self:GetPushedTexture()
			end,
			
			["Type"] = _PropertyType.Texture,
     	},  
  			
		--- FontString
		["FontString"] = {
			["Set"] = function(self, label)
				self:SetFontString(label)
			end,
			
			["Get"] = function(self)
				return self:GetFontString()
			end,
			
			["Type"] = _PropertyType.String,
		},
		
		--- HighlightFontObject
		["HighlightFontObject"] = { 
			["Set"] = function(self, fontObject)
				self:SetHighlightFontObject(fontObject)
			end,

			["Get"] = function(self)
				return self:GetHighlightFontObject()
			end,

			["Type"] = _PropertyType.FontObject,
		}, 
		
		--- NormalFontObject
		["NormalFontObject"] = { 
			["Set"] = function(self, fontObject)
				self:SetNormalFontObject(fontObject)
			end,

			["Get"] = function(self)
				return self:GetNormalFontObject()
			end,

			["Type"] = _PropertyType.FontObject,
		}, 
  	   
		--- PushedTextOffset
		["PushedTextOffset"] = { 
			["Set"] = function(self, offset)
				self:SetPushedTextOffset(offset.x, offset.y)
			end,

			["Get"] = function(self)
				local offset = {}
				offset.x, offset.y = self:GetPushedTextOffset()

				return offset
			end,

			["Type"] = _PropertyType.Offset,
		},
        
		--- Text
		["Text"] = {
			["Set"] = function(self, text)
				self:SetText(text)
			end,
			
			["Get"] = function(self)
				return self:GetText()
			end,
			
			["Type"] = _PropertyType.String,
		},
    
        -- HighlightLocked
        ["HighlightLocked"] = {
			["Set"] = function(self, locked)
                if locked then
				    self.__UI:LockHighlight()
				    self.__HighlightLocked = true
                else
				    self.__UI:UnlockHighlight()
				    self.__HighlightLocked = false
                end
			end,
			
			["Get"] = function(self)
				return (self.__HighlightLocked and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
    }
	
    -- New      
    local _New = function(_parent, inheritFrame)
        -- New Frame
		local frame = CreateFrame(_WidgetName,nil,_BaseFunc.GetUI(_parent),inheritFrame)
				
        return frame
    end
	
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["FuncProxy"] = _FuncProxy,
        ["Property"] = _Property,
        ["ScriptType"] = _ScriptType,
        ["New"] = _New,
    }
    
    _Widget[_WidgetName] = _GUI.NewWidget(_WidgetInfo)
    
	------------------------------------------------------
	--------------- Scan new function --------------------
    ------------------------------------------------------
	local _frame = CreateFrame(_WidgetName,nil,UIParent)
	
	local _oriMeta = getmetatable(_frame)
	_oriMeta = _oriMeta.__index
    
    local _func = _Widget[_WidgetName]["FuncProxy"]
	-- Other Function
	for _f in pairs(_oriMeta) do
		if not _func[_f] and type(_oriMeta[_f]) == "function" then
			_func[_f] = function(self, ...)
                return _BaseFunc.GetVals(self.__UI[_f](self.__UI, _BaseFunc.GetArgs(...)))
			end
		end
	end
	
	-- Clear
	_oriMeta = nil
    _frame:Hide()
	_frame = nil
end